<?php
class OrderReferController extends Controller{

	public function __construct($data) {
		parent::__construct($data);
		$this->_set_head_footer();
	}

	public function index()
	{
		$this->loaddata();
		$this->display('order_refer.html');
	}
    public function loaddata()
    {
    try {
			$orderid=(isset($this->g["oid"])?$this->g["oid"]:(isset($this->p["order_id"])?$this->p["order_id"]:""));
		if($orderid=="")
		{
			throw new Exception("orderid unfound");

		}
		require_once( "../system/Class_M_DAO.php" );
		$orderrow=$this->getorder($orderid);
		if($orderrow['user_id']>=0)
		{
			$userrow=$this->getUser($orderrow['user_id']);
			$this->assign('userrow',$userrow);
		}

		$goods=$this->getgoods($orderid);
		$sumprice=0;
		foreach($goods as $value)
		{
			$sumprice+=$value['goods_unitprice']*$value['goods_num'];
		}

		$this->assign('goods',$goods);
		$this->assign('sumprice',$sumprice);
		$this->assign('orderrow',$orderrow);
		}
		catch(Exception $e)
		{
			$this->assign('errmsg',$e->getMessage());
		}
    }
	public function done()
	{
    if(isset($this->g['oid']))
    {$this->assign('showflg',1);}
    
		$this->assign('orderid',$this->g['oid']);

		$this->display('order_refer_done.html');

	}
	function getorder($oid)
	{
		$M_dao = new Class_M_DAO;

		$sql = "select a.*,
				b.content as pay_content,
				c.content as send_content
				FROM  d_order a
				left join m_orderextraprice b on a.order_pay_id=b.id
				left join m_orderextraprice c on a.order_send_id=c.id
				where order_id=".$oid;

		$orderlist = $M_dao->get_rows($sql);
		return $orderlist[0];
	}

	function getgoods($oid)
	{
		$M_dao = new Class_M_DAO;

		$sql = "select   
                `id`,
                `good_id`,
                `goods_id`,
                `order_id`,
                `goods_name`,
                `goods_image`,
                `goods_unitprice`,
                `goods_num`,
                ifnull(`send_price`,0) as send_price,
                `c_time`,
                `u_time`,
                `c_user`,
                `u_user`
				FROM  d_order_goods a
				where order_id=".$oid;

		$goodlist = $M_dao->get_rows($sql);
		return $goodlist;
	}

	function getUser($userID)
	{
		$M_dao = new Class_M_DAO;

		$sql = "select a.*
				FROM  m_user a
				where id=".$userID;

		$userlist = $M_dao->get_rows($sql);
		if(count($userlist)>0)
		{
		return $userlist[0];
		}
		else {return null;}
	}

	function updatedelete()
	{
		try {
			require_once( "../system/Class_M_DAO.php" );

			$M_dao = new Class_M_DAO;

			$sql = "DELETE FROM d_order where order_id=".$this->p["order_id"];
			if(!$M_dao->exec_sql($sql))
			{
				throw new Exception("updatedelete order error");
			}
			header('Location: Home_OrderManage_index.html');
		}
		catch(Exception $e)
		{
			$this->assign('errmsg',$e->getMessage());
			$this->index();
		}


	}
    
function openupdatesended()
{
    try {
		$orderid=(isset($this->g["oid"])?$this->g["oid"]:(isset($this->p["order_id"])?$this->p["order_id"]:""));
		if($orderid=="")
		{
			throw new Exception("orderid unfound");

		}
		require_once( "../system/Class_M_DAO.php" );
        	$M_dao = new Class_M_DAO;

            $idarray=explode("-",$orderid);
            $where="";
            foreach($idarray as $value)
            {
            if($where=="")
            {
            $where="a.order_id=".$value;
            }
            else
            {
            $where.=" or a.order_id=".$value;
            }
            }
		$sql = "select a.*,
				b.content as pay_content,
				c.content as send_content
				FROM  d_order a
				left join m_orderextraprice b on a.order_pay_id=b.id
				left join m_orderextraprice c on a.order_send_id=c.id
				where ".$where;
		$orderrow = $M_dao->get_rows($sql);
		
		$this->assign('orderrow',$orderrow);
		}
        
		catch(Exception $e)
		{
			$this->assign('errmsg',$e->getMessage());
		}
       $this->assign('orderids',$orderid);
		$M_dao = new Class_M_DAO;

		$sql4 ="SELECT id,content FROM m_orderextraprice  where  category='0' ";

		$order_buy = $M_dao->get_rows($sql4);
        $this->assign('order_buy',$order_buy);
        if(!empty($this->g["muti"]))
        {
            $this->assign('backurl',"Home_OrderManage_index.html");
             $this->assign('mutiflag',"?muti=1");
        }
        else
        {
        $this->assign("backurl","Home_OrderRefer_index.html?oid=".$orderrow[0]["order_id"]);
        }
    $this->display('order_refer_updatesended.html');
}
	function updatesended($i_dao=null)
	{
        require_once( "../system/Class_M_DAO.php" );
        $M_dao = new Class_M_DAO;
        $M_dao->begin_trans();
		try {
       
        
          $idarray=explode("-",$this->p["order_id"]);
         
           foreach($idarray as $value)
            {
			    $sql = "update d_order SET order_status='3' ,
					    sent_datetime=IF('".$this->p[$value."_sent_datetime"]."'='',null,'".$this->p[$value."_sent_datetime"]."'),
					    delivery_company='".$this->p[$value."_delivery_company"]."',
					    delivery_number='".$this->p[$value."_delivery_number"]."',
					    delivery_app='".$this->p[$value."_delivery_app"]."'
					    where order_id=".$value;
			    if(!$M_dao->exec_sql($sql))
			    {
				    throw new Exception("updatesended order error");
			    }
           }
           
			$M_dao->commit_trans();
             if(!empty($this->g["muti"]))
            {
                header('Location: Home_OrderRefer_done.html');
            }
            else
            {
			header('Location: Home_OrderRefer_done.html?oid='.$this->p['order_id']);
            }
		}
		catch(Exception $e)
		{
        	$M_dao->rollback_trans();
			$this->assign('errmsg',$e->getMessage());
			$this->index();
		}

	}
    
    function updatestatus()
    {
     try {
			require_once( "../system/Class_M_DAO.php" );
			$M_dao=null;
			if(empty($i_dao))
			{$M_dao = new Class_M_DAO;}
			else
			{$M_dao=$i_dao;}
			$sql = "update d_order SET order_status='".$this->p["order_status"]."' 
					where order_id=".$this->p["order_id"];
			if(!$M_dao->exec_sql($sql))
			{
				throw new Exception("updatesended order error");
			}
           
			$this->turntoindex();
		}
		catch(Exception $e)
		{
			$this->assign('errmsg',$e->getMessage());
		
			$this->index();
		}
    }
function quickupdatesended()
{
        try {
			require_once( "../system/Class_M_DAO.php" );
			$M_dao=null;
			if(empty($i_dao))
			{$M_dao = new Class_M_DAO;}
			else
			{$M_dao=$i_dao;}
			$sql = "update d_order SET order_status='3' 
					where order_id=".$this->p["order_id"];
			if(!$M_dao->exec_sql($sql))
			{
				throw new Exception("updatesended order error");
			}
           
			$this->turntoindex();
		}
		catch(Exception $e)
		{
			$this->assign('errmsg',$e->getMessage());
		
			$this->index();
		}

}
function openupdaterequestaddress()
{
    $this->loaddata();
    $this->display('order_refer_updaterequestaddress.html');
}
	function updaterequestaddress($i_dao=null)
	{
		try {
			require_once( "../system/Class_M_DAO.php" );
			$M_dao=null;
			if(empty($i_dao))
			{$M_dao = new Class_M_DAO;}
			else
			{$M_dao=$i_dao;}
			$sql = "update d_order SET
					user_name1='".$this->p["user_name1"]."',
					user_name2='".$this->p["user_name2"]."',
					user_office_name='".$this->p["user_office_name"]."',
					user_local_name='".$this->p["user_local_name"]."',
					user_treet_name='".$this->p["user_treet_name"]."',
					user_address='".$this->p["user_address"]."',
					user_tel_number='".$this->p["user_tel_number"]."',
					email='".$this->p["email"]."'
					where order_id=".$this->p["order_id"];
			if(!$M_dao->exec_sql($sql))
			{
				throw new Exception("updatesended order error");
			}
			$this->turntoindex();
			}
			catch(Exception $e)
			{
				$this->assign('errmsg',$e->getMessage());
			
				$this->index();
			}

	}
function openupdatesendaddress()
{
    $this->loaddata();
    $this->display('order_refer_updatesendaddress.html');
}
	function updatesendaddress($i_dao=null)
	{
		try {
			require_once( "../system/Class_M_DAO.php" );
			$M_dao=null;
			if(empty($i_dao))
			{$M_dao = new Class_M_DAO;}
			else
			{$M_dao=$i_dao;}
			$sql = "update d_order SET
					send_name1='".$this->p["send_name1"]."',
					send_name2='".$this->p["send_name2"]."',
					send_office_name='".$this->p["send_office_name"]."',
					send_local_name='".$this->p["send_local_name"]."',
					send_treet_name='".$this->p["send_treet_name"]."',
					send_address='".$this->p["send_address"]."',
					send_tel_number='".$this->p["send_tel_number"]."'
					where order_id=".$this->p["order_id"];
			if(!$M_dao->exec_sql($sql))
			{
				throw new Exception("updatesended order error");
			}
			$this->turntoindex();
			}
			catch(Exception $e)
			{
				$this->assign('errmsg',$e->getMessage());
			
				$this->index();
			}
	}
function openupdateothers()
{
    $this->loaddata();
    $this->display('order_refer_updateothers.html');
}
	function updateothers($i_dao=null)
	{
		try {
			require_once( "../system/Class_M_DAO.php" );
			$M_dao=null;
			if(empty($i_dao))
			{$M_dao = new Class_M_DAO;}
			else
			{$M_dao=$i_dao;}
			$sql = "update d_order SET
					order_appraise='".$this->p["order_appraise"]."',
					insideappraise='".$this->p["insideappraise"]."'
					where order_id=".$this->p["order_id"];
			if(!$M_dao->exec_sql($sql))
			{
				throw new Exception("updatesended order error");
			}
			$this->turntoindex();
			}
			catch(Exception $e)
			{
				$this->assign('errmsg',$e->getMessage());
			
				$this->index();
			}
	}
function openupdatesumprice()
{
    $this->loaddata();
    $this->display('order_refer_updatesumprice.html');
}
	function updatesumprice($i_dao=null)
	{

		try {
			require_once( "../system/Class_M_DAO.php" );
			$M_dao=null;
			if(empty($i_dao))
			{$M_dao = new Class_M_DAO;}
			else
			{$M_dao=$i_dao;}
			$sql = "update d_order SET
					send_price=".$this->p["send_price"].",
					buying_price=".$this->p["buying_price"].",
					tax=".$this->p["tax"].",
                    goods_sum=".$this->p["goods_sum"].",
                    all_sum=".$this->p["all_sum"]."
					where order_id=".$this->p["order_id"];
			if(!$M_dao->exec_sql($sql))
			{
				throw new Exception("updatesended order error");
			}
			$this->turntoindex();
			}
			catch(Exception $e)
			{
				$this->assign('errmsg',$e->getMessage());
			
				$this->index();
			}
	}
function openupdategoods()
{
    $this->loaddata();
    $this->display('order_refer_updategoods.html');
}
	function updategoods($i_dao=null)
	{
		require_once( "../system/Class_M_DAO.php" );
        $M_dao = new Class_M_DAO;
        $M_dao->begin_trans();
		try {

			if(empty($i_dao))
			{$M_dao = new Class_M_DAO;$M_dao->begin_trans();}
			else
			{$M_dao=$i_dao;}
            $good_sum=0;
			for($i=0;$i<$this->p['goodcount'];$i++)
			{

				$sql = "update d_order_goods SET
					goods_unitprice=".$this->p["goods_unitprice_".$i].",
					goods_num=".$this->p["goods_num_".$i].",
                    send_price=".$this->p["goods_sendprice_".$i]."
					where id=".$this->p["goodid_".$i];
				if(!$M_dao->exec_sql($sql))
				{
					throw new Exception("updatesended order error");
				}
                $good_sum+=($this->p["goods_unitprice_".$i]*$this->p["goods_num_".$i]+$this->p["goods_sendprice_".$i]);
			}
            $orderid=(isset($this->g["oid"])?$this->g["oid"]:(isset($this->p["order_id"])?$this->p["order_id"]:""));
            
            $sql="UPDATE 
              `d_order` a
            SET 
              a.`goods_sum` = ".$good_sum.",
              a.`all_sum` = a.`buying_price`+a.`send_price`+a.`tax`+".$good_sum."
            WHERE 
              a.`order_id` = ".$orderid;
            if(!$M_dao->exec_sql($sql))
			{
				throw new Exception("updatesended order error");
			}
			$M_dao->commit_trans();

			$this->turntoindex();
		}
		catch(Exception $e)
		{
			$M_dao->rollback_trans();
			$this->assign('errmsg',$e->getMessage());
			$this->index();
		}
	}

	function updateall()
	{
		require_once( "../system/Class_M_DAO.php" );
		$M_dao = new Class_M_DAO;
		try {


			$M_dao->begin_trans();

			$this->updatesended($M_dao);
			$this->updatesendaddress($M_dao);
			$this->updaterequestaddress($M_dao);
			$this->updateothers($M_dao);
			$this->updatesumprice($M_dao);
			$this->updategoods($M_dao);

			$M_dao->commit_trans();
			$this->turntoindex();
		}
		catch(Exception $e)
		{
			$M_dao->rollback_trans();
			$this->assign('errmsg',$e->getMessage());
			$this->index();
		}

	}

	function turntoindex()
	{
		header('Location: Home_OrderRefer_done.html?oid='.$this->p['order_id']);
	}
}